<?php
/**
 * Gold Cmage Module
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Gold
 * @package     Gold_Cmage
 * @copyright   Copyright (c) 2011 Gold
 * @Author $Id$
*/

/**
 * Product list
 *
 * @category   Gold
 * @package    Gold_Cmage
 */
class Gold_Cmage_Block_Catalog_Product_List extends Mage_Catalog_Block_Product_List
{
    /**
     * Prepare Sort By fields from Category Data
     *
     * @param Mage_Catalog_Model_Category $category
     * @return Mage_Catalog_Block_Product_List
     */
    public function prepareSortableFieldsByCategory($category) {
        if (!$this->getAvailableOrders()) {
            $this->setAvailableOrders(array_merge($category->getAvailableSortByOptions(),array('created_at'=>$this->__('Date'))));
        }        
        return parent::prepareSortableFieldsByCategory($category);
    }
    
	public function getDefaultOrderDirection(){
		return array('created_at' => 'desc');
    }
    
    public function getOrderDirection()
    {
    	$orderDirection = array();    	
    	$availableOrders = $this->getAvailableOrders();
    	if (empty($availableOrders))
    	{
    		return array();
    	}
    	$order = $this->getRequest()->getParam('order');
    	$dir = $this->getRequest()->getParam('dir');
    	foreach ($availableOrders as $_key=>$_order)
    	{
    		if (!empty($order) && $order == $_key)
    		{
    			if ($dir == 'desc')
    			{
    				$orderDirection[$_key] = 'asc';
    			}
    			else
    			{
    				$orderDirection[$_key] = 'desc';
    			}
    		}
    		else
    		{
    			if ($defaultOrderDir = $this->getDefaultOrderDirection())
    			{
    				$orderDirection[$_key] = $defaultOrderDir[$_key];
    			}
    		}
    	}
    	
    	return $orderDirection;
    }
    
 	/**
     * Need use as _prepareLayout - but problem in declaring collection from
     * another block (was problem with search result)
     */
    protected function _beforeToHtml()
    {
        $toolbar = $this->getToolbarBlock();
		// called prepare sortable parameters
        $collection = $this->_getProductCollection();
        if ($orderDir = $this->getOrderDirection()) {
            $toolbar->setOrderDirection($orderDir);
        }
        return parent::_beforeToHtml();
    }
}
